package com.bbcare.exportExcel.plat.vo;

import java.util.*;

public class DataExcelSheet implements Comparable   {
    private String name;    //sheet name.
    //1个sheet可能会用多个模板，比如月龄随访任务里面还用到随访指导的模板。
//    private List<String> templVersionIdList = new ArrayList<>();
    private Map<String,String> templveridMap= new HashMap<>();

    //key is userId。
    private Map<String, DataExcelRow> rowsMap = new HashMap<>();
    private List<String> rowidList  = new ArrayList<>();

    private Integer seqNo;

    public DataExcelSheet(final String sheetName)   {
        this.name   = sheetName;
    }

    public void setSeqNo(final Integer no)  {this.seqNo=no;}
    public Integer getSeqNo()   {return this.seqNo;}

/*-----------------------------------------------------------------------------*/
    public void addTemplVersionId(final String id)  {
        if (!this.templveridMap.containsKey(id)) {
            //this.templVersionIdList.add(id);
            this.templveridMap.put(id, "");
        }
    }
    public List<String> getTemplVersionIdList() {
        List<String> templVersionIdList = new ArrayList<>();
        for (Map.Entry<String, String> entry : this.templveridMap.entrySet()) {
            templVersionIdList.add(entry.getKey());
        }
        Collections.sort(templVersionIdList);
        return templVersionIdList;
    }


/*-----------------------------------------------------------------------------*/

    public void setName(final String name)  {this.name=name;}
    public String getName() {return this.name;}

    public boolean contains(final String userId)    {
        return rowsMap.containsKey(userId);
    }

    public void addRow(DataExcelRow row)    {
        final String rowId  = row.getRowId();   //rowId就是userId.
        if (!contains(rowId)) {
            rowsMap.put(rowId, row);
            rowidList.add(rowId);
        }
    }

    public DataExcelRow getRow(final String rowId) {
        return rowsMap.get(rowId);
    }

    public int size()   {
        return rowidList.size();
    }
/*
    public String toString()    {
        StringBuffer sb = new StringBuffer();
        Set<String> keys= this.rows.keySet();
        Iterator<String> iter   = keys.iterator();
        while (iter.hasNext())  {
            sb.append(this.rows.get(iter.next()).toString());
        }
        return sb.toString();
    }

    public List<String> getRowIds() {
        List<String> retList= new ArrayList<>();
        Set<String> keys    = this.rowsMap.keySet();
        Iterator<String> iter   = keys.iterator();
        while (iter.hasNext())  {
            retList.add(iter.next());
        }

        return retList;
    }
*/
    public List<String> getRowIds() {
        return this.rowidList;
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof DataExcelSheet)    {
            DataExcelSheet b= (DataExcelSheet)o;
            int bSeqNo  = b.getSeqNo().intValue();
            if (getSeqNo().intValue()<bSeqNo)   {
                return -1;
            } else if (getSeqNo().intValue()>bSeqNo)   {
                return 1;
            } else {
                return 0;
            }
        }

        return -1;
    }

    /**
     * sheet中的行排序（根据随访日期降序）。
     */
    public void sortRowsByFuDate()  {
        //这⾥将map.entrySet()转换成list
        List<Map.Entry<String,DataExcelRow>> list = new ArrayList<Map.Entry<String,DataExcelRow>>(this.rowsMap.entrySet());
        //然后通过⽐较器来实现排序
        Collections.sort(list, new Comparator<Map.Entry<String,DataExcelRow>>() {
            //升序排序
            public int compare(Map.Entry<String, DataExcelRow> o1, Map.Entry<String, DataExcelRow> o2) {
                final String fuDate1    = o1.getValue().getFollowupDate();
                final String fuDate2    = o2.getValue().getFollowupDate();
                int ret   = fuDate1.compareTo(fuDate2);
                if (0 != ret)   {
                    ret = -ret;
                }
                return ret;
            }
        });

        this.rowidList.clear();
        for(Map.Entry<String,DataExcelRow> row : list)   {
            rowidList.add(row.getValue().getRowId());
        }
    }
}
